<template>
  <div class="home">
    <el-button @click="logout">退出登录</el-button>
    <el-button type='primary' @click="getData">请求数据</el-button>
    
    <HelloWorld msg="Welcome to Your Vue.js App" />
  </div>
</template>

<script>
// @ is an alias to /src
import HelloWorld from "@/components/HelloWorld.vue";

export default {
  name: "HomeView",
  components: {
    HelloWorld,
  },
  methods: {
    logout() {
      window.localStorage.setItem("username",'');
      this.$router.push("/login");
    },
    getData(){
      this.$axios.get('/data')
      .then(res=>{
        console.log('res',res);
      })
      .catch(error=>{
        console.error('error',error);
      })
    }
  },
  created() {
    let username = window.localStorage.getItem("username");
    if (!username) {
      // 未登录
      this.$router.push("/login");
    }
  },
};
</script>
<style scoped>
</style>
